package dice.sinanya.db.heap;

import dice.sinanya.db.imal.CheckQqId;
import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.exceptions.CantFindQqIdException;
import dice.sinanya.system.MessagesSystem;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nlpcn.commons.lang.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static dice.sinanya.db.system.SelectBot.selectBot;
import static dice.sinanya.system.MessagesSystem.loginInfo;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityBanProperties;
import static dice.sinanya.tools.getinfo.GetTime.getNowString;
import static dice.sinanya.tools.getinfo.GetTime.getTime;

/**
 * @author SitaNya
 * 日期: 2019-08-09
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明:
 */
public class InsertHeap implements CheckQqId {
    private static final Logger log = LogManager.getLogger(InsertHeap.class.getName());


    /**
     * 将QQ黑名单列表入库
     */
    public void updateHeap() throws CantFindQqIdException {
        String botId = checkQqId();
        int retryTimes = 0;
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "replace into test.heap(botId,botName,time,enable,botMaster,version,groupNum)  VALUES (?,?,?,?,?,?,?);";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, botId);
                ps.setString(2, loginInfo.getLoginName());
                ps.setTimestamp(3, getTime(getNowString()));
                ps.setBoolean(4, entityBanProperties.isHeap());
                ps.setString(5, StringUtil.joiner(entityBanProperties.getMaster(), ","));
                ps.setString(6, MessagesSystem.VERSIONS);
                ps.setInt(7, selectBot().getGroupNum());
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }
}
